package aom.ju.ss.view.user.shop.management

import android.R.attr
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.LinearLayoutManager
import aom.ju.ss.R
import aom.ju.ss.databinding.PublishVideoBinding
import aom.ju.ss.databinding.ShopIteBinding
import aom.ju.ss.lpInterface.BindViewInterface
import aom.ju.ss.mode.ShopManngerGoodsList
import aom.ju.ss.mode.ShopManngerGoodsListData
import aom.ju.ss.utils.http.LP_API
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.utils.show
import aom.ju.ss.view.base.BaseActivity
import aom.ju.ss.view.base.PAdapter
import com.google.gson.Gson
import android.content.Intent
import android.provider.MediaStore
import android.R.attr.data
import android.content.Context
import android.database.Cursor
import android.net.Uri
import android.util.Log
import aom.ju.ss.utils.LogUtils
import aom.ju.ss.utils.log
import java.lang.Exception
import androidx.core.app.ActivityCompat.startActivityForResult
import android.media.ThumbnailUtils

import android.graphics.Bitmap

import android.webkit.MimeTypeMap


/**
 * 发布视频
 */
class PublishVideo : BaseActivity() {
    private lateinit var binding: PublishVideoBinding

    private lateinit var padapter: PAdapter
    private var list = arrayListOf<ShopManngerGoodsListData>()
    override fun init() {
        binding = viewBinding as PublishVideoBinding
        OKHttpUtils(activity)
            .SetApiUrl(LP_API.shop_goods_get_list)
            .POST(this)
        padapter = PAdapter(
            list, R.layout.shop_ite
        ) { b, position ->
            val ib = b as ShopIteBinding
            val info = list[position]
            ib.info = info
            ib.rootView.setOnClickListener {
                if (!list[position].ck) {
                    var num2 = 0
                    for (a in list) {
                        if (num2 == 2) {
                            "最多选择两个商品".show
                            return@setOnClickListener
                        }
                        if (a.ck) {
                            num2++
                        }
                    }
                }
                list[position].ck = !list[position].ck
                padapter.notifyItemChanged(position)
            }
        }
        binding.rec.layoutManager = LinearLayoutManager(activity)
        binding.rec.adapter = padapter
        binding.xz.setOnClickListener { }
        binding.fabu.setOnClickListener { }
        binding.img.setOnClickListener {
//            val intent = Intent()
//            intent.action = Intent.ACTION_GET_CONTENT
//            intent.setDataAndType(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, "video/*")
//            activity!!.startActivityForResult(intent, 1998)

            val intent = Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI)
            startActivityForResult(intent, 1998)
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            if (requestCode == 1998) {
                data?.data?.let {
                    val path = getRealPathFromUriVideo(activity!!, it)

                    val fileExtension = MimeTypeMap.getFileExtensionFromUrl(path)
                    val mimeType =
                        MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension)
                    if (mimeType != null && mimeType.contains("video")) {


                    } else {
                        "请选择视频".show
                    }

                }


            }
        }
    }

    fun getRealPathFromUriVideo(context: Context, contentUri: Uri): String {
        var cursor: Cursor? = null
        try {
            LogUtils.e("getRealPathFromUri: $contentUri")
            val proj = arrayOf(MediaStore.Video.Media.DATA)
            cursor = context.contentResolver.query(contentUri, proj, null, null, null)
            if (cursor != null && cursor.columnCount > 0) {
                cursor.moveToFirst()
                val column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA)
                val path = cursor.getString(column_index)
                LogUtils.e("getRealPathFromUri: column_index=$column_index, path=$path")
                return path
            } else {
                LogUtils.e("getRealPathFromUri: invalid cursor=$cursor, contentUri=$contentUri")
            }
        } catch (e: Exception) {
            LogUtils.e("getRealPathFromUri failed: $e, contentUri=$contentUri" + e.message)
        } finally {
            cursor?.close()
        }
        return ""
    }

    override fun ok(jsonString: String, httpTY: String) {
        super.ok(jsonString, httpTY)
        val mode = Gson().fromJson(jsonString, ShopManngerGoodsList::class.java)
        mode.data?.let {
            list.addAll(it)
            padapter.notifyDataSetChanged()
        }
    }

    override fun error(e: String, jsonString: String, httpTY: String) {
        super.error(e, jsonString, httpTY)
        e.show
    }

    override val lay: Int = R.layout.publish_video
}